package com.sky.workspace.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dish.service.DishService;
import com.sky.entity.Orders;
import com.sky.orders.service.OrderService;
import com.sky.setmeal.service.SetMealService;
import com.sky.user.service.UserService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.workspace.service.WorkSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 工作台 serviceImpl
 */
@Slf4j
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {



    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @Resource
    private DishService dishService;

    @Resource
    private SetMealService setMealService;




    /**
     * 工作台
     *        今日运营数据
     * @param startTime
     * @param endTime
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime startTime, LocalDateTime endTime) {
        // 封装查询参数
        Map map = new ConcurrentHashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        //当天 订单数总数
        Integer dayTotalOrderCount = orderService.getOrderStatisticsByMap(map);
        dayTotalOrderCount = dayTotalOrderCount == null ? 0 : dayTotalOrderCount;

        //当天 新增用户量
        Integer dayAddUserCount = userService.selectUserCountByMap(map);
        dayAddUserCount = dayAddUserCount == null ? 0 : dayAddUserCount;


        map.put("status", Orders.COMPLETED); // 已完成status=5

        //当天 已完成订单数
        Integer daySuccessOrderCount = orderService.getOrderStatisticsByMap(map);
        daySuccessOrderCount = daySuccessOrderCount == null ? 0 : daySuccessOrderCount;

        //当天 已完成订单的-营业额
        BigDecimal dayTurnOver = orderService.selectDayTotalAmountByMap(map);
        dayTurnOver = dayTurnOver == null ? new BigDecimal("0.00") : dayTurnOver;

        //订单完成率
        Double orderCompletionRate = 0.00;
        //平均订单价
        Double unitPrice = 0.00;

        //计算
        if(dayTotalOrderCount != 0 && daySuccessOrderCount != 0){
            // 完成率 = 已完成订单数 / 总数
            BigDecimal success = new BigDecimal(daySuccessOrderCount.toString());
            BigDecimal total = new BigDecimal(dayTotalOrderCount.toString());
            orderCompletionRate = success.divide(total, 2, BigDecimal.ROUND_HALF_DOWN).doubleValue();

            // 平均单价 = 营业额 / 已完成订单   营业额已经是BigDecimal类型
            BigDecimal successOrder = new BigDecimal(daySuccessOrderCount.toString());
            unitPrice = dayTurnOver.divide(successOrder, 2 ,BigDecimal.ROUND_HALF_DOWN).doubleValue();
        }

        // 返回
        return BusinessDataVO.builder()
                .turnover(dayTurnOver.doubleValue())
                .validOrderCount(daySuccessOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(dayAddUserCount)
                .build();
    }



    /**
     * 工作台
     *      今日订单管理
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        // 封参
        Map map = new ConcurrentHashMap();
        map.put("startTime", LocalDateTime.now().with(LocalTime.MIN));   // yyyy-MM-dd 00:00:00

        // 今天所有订单
        // SQL: select count(id) from orders where order_time >= #{startTime}
        Integer dayOrderCount = orderService.getOrderStatisticsByMap(map);
        dayOrderCount = dayOrderCount == null ? 0 : dayOrderCount;

        // 待接单 2
        // SQL: select count(id) from orders where order_time >= #{startTime} and status = #{status}
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderService.getOrderStatisticsByMap(map);
        waitingOrders = waitingOrders == null ? 0 : waitingOrders;
        
        // 待派送 3
        map.put("status", Orders.CONFIRMED);
        Integer deliveryOrders = orderService.getOrderStatisticsByMap(map);
        deliveryOrders = deliveryOrders == null ? 0 : deliveryOrders;

        // 已完成 5
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderService.getOrderStatisticsByMap(map);
        completedOrders = completedOrders == null ? 0 : completedOrders;

        // 已取消 6
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderService.getOrderStatisticsByMap(map);
        cancelledOrders = cancelledOrders == null ? 0 : cancelledOrders;


        // 返回
        return OrderOverViewVO.builder()
                .allOrders(dayOrderCount)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveryOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
    }



    /**
     * 工作台
     *      菜品总览
     */
    @Override
    public DishOverViewVO getDishOverView() {
        // 封参
        Map map = new ConcurrentHashMap();
        map.put("status", StatusConstant.DISABLE); // 停售
        // SQL: select count(id) from dish where status = #{status}
        Integer discontinued = dishService.countDishStatusByMap(map);
        discontinued = discontinued == null ? 0 : discontinued;


        map.put("status", StatusConstant.ENABLE);  // 起售
        Integer sold = dishService.countDishStatusByMap(map);
        sold = sold == null ? 0 : sold;

        // 返回
        return DishOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }



    /**
     * 工作台
     *      套餐总览
     */
    @Override
    public SetmealOverViewVO setmealOverView() {
        // 封参
        Map map = new ConcurrentHashMap();
        map.put("status", StatusConstant.DISABLE);  // 停售
        // SQL: select count(id) from setmeal where status = #{status}
        Integer discontinued = setMealService.countSetmealStatusByMap(map);
        discontinued = discontinued == null ? 0 : discontinued;

        map.put("status", StatusConstant.ENABLE);   // 起售
        Integer sold = setMealService.countSetmealStatusByMap(map);
        sold = sold == null ? 0 : sold;

        // 返回
        return SetmealOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }
}
